| Total Complexity | 7 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { Inject } from '@nestjs/common'; |
||
| 7 | |||
| 8 | export class FairCalendarOverviewFactory { |
||
| 9 | constructor( |
||
| 10 | @Inject('ICooperativeRepository') |
||
| 11 | private readonly cooperativeRepository: ICooperativeRepository |
||
| 12 | ) {} |
||
| 13 | |||
| 14 | public async create(items: FairCalendarView[]): Promise<ICalendarOverview> { |
||
| 15 | const cooperative = await this.cooperativeRepository.find(); |
||
| 16 | if (!cooperative) { |
||
| 17 | throw new CooperativeNotFoundException(); |
||
| 18 | } |
||
| 19 | |||
| 20 | const overviewInDays: ICalendarOverview = { |
||
| 21 | mission: { |
||
| 22 | days: 0, |
||
| 23 | details: [] |
||
| 24 | }, |
||
| 25 | dojo: { |
||
| 26 | days: 0 |
||
| 27 | }, |
||
| 28 | formationConference: { |
||
| 29 | days: 0 |
||
| 30 | }, |
||
| 31 | leave: { |
||
| 32 | days: 0 |
||
| 33 | }, |
||
| 34 | support: { |
||
| 35 | days: 0 |
||
| 36 | }, |
||
| 37 | other: { |
||
| 38 | days: 0 |
||
| 39 | } |
||
| 40 | }; |
||
| 41 | |||
| 42 | for (const { time, type: itemType, project } of items) { |
||
| 43 | const type = itemType.startsWith('leave_') ? 'leave' : itemType; |
||
| 44 | const days = time / cooperative.getDayDuration(); |
||
| 45 | |||
| 46 | if (overviewInDays[type]) { |
||
| 47 | overviewInDays[type].days = |
||
| 48 | Math.round((overviewInDays[type].days + days) * 100) / 100; |
||
| 49 | |||
| 50 | if (type === EventType.MISSION) { |
||
| 51 | const missionDetail = overviewInDays[type].details.find( |
||
| 52 | ({ label }) => label === project.name |
||
| 53 | ); |
||
| 54 | |||
| 55 | if (missionDetail) { |
||
| 56 | missionDetail.days = |
||
| 57 | Math.round( |
||
| 58 | (missionDetail.days + Math.round(days * 100) / 100) * 100 |
||
| 59 | ) / 100; |
||
| 60 | } else { |
||
| 61 | overviewInDays[type].details.push({ |
||
| 62 | days, |
||
| 63 | label: project.name |
||
| 64 | }); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return overviewInDays; |
||
| 71 | } |
||
| 73 |